package com.soulkun.smarthome.ui.main.personal.dialog

import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppDialogMainPersonalFamilyRoleSetBinding
import com.soulkun.smarthome.databinding.AppRecycleItemMainPersonalFamilySetRoleListBinding
import com.soulkun.smarthome.logic.model.RoleModel
import com.soulkun.smarthome.logic.vo.RoleVO
import com.soulkun.smarthome.ui.main.personal.adapter.AppMainPersonalFamilySetRoleListAdapter
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilySetRoleViewModel
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalFamilySettingViewModel
import soulkun.library.mvvm.adapter.clearAndAddAll
import soulkun.library.mvvm.component.BaseDataBindingDialogFragment
import soulkun.library.utils.toPx
import soulkun.library.utils.toast

class AppMainPersonalFamilyRoleSetDialog(private val dataViewModel: AppMainPersonalFamilySettingViewModel) :
    BaseDataBindingDialogFragment<AppDialogMainPersonalFamilyRoleSetBinding>() {

    private val viewModel by lazy {
        ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory(requireActivity().application)
        ).get(AppMainPersonalFamilySetRoleViewModel::class.java)
    }
    private val adapter by lazy {
        AppMainPersonalFamilySetRoleListAdapter(requireContext(), viewModel.roleList) { binding ->
            View.OnClickListener {
                if (dataViewModel.selectFamilyUserLiveData.value!!.role.get()!!.id != (binding as AppRecycleItemMainPersonalFamilySetRoleListBinding).role!!.id) {
                    dataViewModel.setFamilyUserRole(binding.role!!.id)
                        .observe(viewLifecycleOwner) { result ->
                            val result = result.getOrNull()
                            if (result != null) {
                                result.statusMessage.toast()
                                viewModel.roleList.forEach {
                                    if (it.isSelected.get()) it.isSelected.set(false)
                                    if (it.id == binding.role!!.id) it.isSelected.set(true)
                                }
                                if (binding.role!!.roleKey == "family_host") {
                                    val admin =
                                        viewModel.roleList.find { it.roleKey == "family_admin" }!!
                                    dataViewModel.familyMemberList.find { it.role.get()!!.roleKey == "family_host" }?.role!!.set(
                                        RoleModel(admin.id, admin.name, admin.roleKey, remark = admin.remark)
                                    )
                                }
                                dataViewModel.selectFamilyUserLiveData.value!!.role.set( RoleModel(binding.role!!.id, binding.role!!.name, binding.role!!.roleKey, remark = binding.role!!.remark) )
                                dismissAllowingStateLoss()
                            }
                        }
                }
            }
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_dialog_main_personal_family_role_set
    }

    override fun doOnViewCreated() {
        requireDialog().window?.run {
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, requireContext().resources.getDimensionPixelSize(R.dimen.sw_250dp))
            setGravity(Gravity.BOTTOM)
        }
        isCancelable = true
        requireDataBinding().rvMainPersonalFamilySetRoleListRole.run {
            layoutManager = LinearLayoutManager(context)
            adapter = this@AppMainPersonalFamilyRoleSetDialog.adapter
        }
    }

    override fun doOnResume() {
        viewModel.getFamilyRoleList().observe(viewLifecycleOwner) { result ->
            val result = result.getOrNull()
            if (result != null) {
                val list = result.data.map { roleModel ->
                    RoleVO(
                        roleModel.id!!,
                        roleModel.name!!,
                        roleModel.roleKey!!,
                        roleModel.remark!!
                    )
                }
                list.find { roleVO -> roleVO.id == dataViewModel.selectFamilyUserLiveData.value!!.role.get()!!.id }!!.isSelected.set(
                    true
                )
                viewModel.roleList.clearAndAddAll(list)
            }
        }
    }

    override fun doOnDismiss() {
    }

    override fun doOnDestroyView() {
    }
}